﻿using System;
using System.Collections.Generic;
using ZSoftInstagram.DataAccess.Abstractions;
using ZSoftInstagram.Domain.Board;
using ZSoftInstagram.Services.Abstraction;
using ZSoftInstagram.UI.ViewModels;
using System.Linq;
using System.Data.Entity;
using System.Threading.Tasks;
using ZSoftInstagram.Domain;

namespace ZSoftInstagram.Services
{
    public class PinBoardService : IPinBoardService
    {
        private IRepository<ImagePinBoard> _repository;
        public PinBoardService(IRepository<ImagePinBoard> repository)
        {
            this.Repository = repository;
        }

        public IRepository<ImagePinBoard> Repository
        {
            get
            {
                return _repository;
            }
            private set
            {
                this._repository = value;
            }
        }

        public Task<PinBoardEditModel> GetBoardAsync(int id)
        {
            return  _repository.Where(pb => pb.Id == id, "Pins")
                                    .Select(p => new PinBoardEditModel()
                                        {
                                            Id = p.Id,
                                            Title = p.Name,
                                            Pins = p.Pins.Select(pi => new PinViewModel()
                                            {
                                                Id = pi.Id,
                                                Address = new AddressViewModel()
                                                {
                                                    AddressType = pi.Address.AddresType, Path = pi.Address.Path
                                                },
                                                Description = pi.Description,
                                                Date = pi.Date
                                            }).ToList()
                                    }).FirstAsync();
        }
        
        public int Create(PinBoardCreateModel pinBoard)
        {
            throw new NotImplementedException();
        }

        public Task<int> CreateAsync(PinBoardCreateModel model)
        {
            return _repository.Add(new ImagePinBoard()
            {
                Name = model.Title,
                Date = model.Date,
                Description = model.Description,
                UserId = model.UserId,
                Categories = new List<Category>() { this._repository.GetTypeRepository<Category>().First(c => c.Id == model.CategoryId) }
            }).SaveChangesAsync();

        }
        
        public Task<List<PinBoardDashBoardModel>> GetUserBoardsAsync(int userId)
        {
            return this.Repository.Where(p => p.UserId == userId)
                                    .Select(p => new PinBoardDashBoardModel
                                    {
                                        Id = p.Id,
                                        Description = p.Description,
                                        Date = p.Date,
                                        Categories = p.Categories.Select(pc => new CategoryViewModel()
                                        {
                                            Id = p.Id,
                                            Title = pc.Title
                                        }).ToList(),
                                        Title = p.Name,
                                    }).ToListAsync();
        }

        public void Remove(PinBoardDeleteModel pinBoard)
        {
            throw new NotImplementedException();
        }

        public void Update(PinBoardEditModel pinBoard)
        {
            throw new NotImplementedException();
        }

        public PinBoardCreateViewModel GetCreateModel()
        {
            return new PinBoardCreateViewModel()
            {
                Categories = this._repository.GetTypeRepository<Category>().All().Select(c => new CategoryViewModel()
                {
                    Id = c.Id,
                    Title = c.Title
                }).ToList()
            };
        }
    }
}
